import tornado.web
import json
import tornado.httpserver
from app.dbconfig.databasemethod import queryofferall,querylimitoffer,queryappid,querynetid
from app.dbconfig.setting_header import setheader
class offerlist(setheader):

    def get(self):
        #接收前端传输的分页以及大小
        pageNum = self.get_argument("pageNum",1)
        pageSize = self.get_argument("pageSize",100)

        pageNum = int(pageNum)
        pageSize = int(pageSize)
        res=dict()
        #查询总条数
        offer=queryofferall()
        #分页查询
        data=querylimitoffer(pageSize,(pageNum-1)*pageSize)

        if data==[]:
            res["isSuccess"]=False
            res["msg"]="error"
        else:
            res["isSuccess"]=True
            res["msg"]="success"
            data1 = []
            #遍历数据添加到数组
            for k in data:
                data2 = []

                data2.append(k.ad_id)
                data2.append(k.ad_name)
                netdata = querynetid(k.net_id)
                if netdata==None:
                    data2.append("")
                else:

                    data2.append(netdata.net_name)
                appdata = queryappid(k.app_id)
                if appdata == None:
                    data2.append("")
                else:
                    data2.append(appdata.app_name)
                print(appdata)
                data2.append(k.redirect_url)
                data2.append(k.net_id)
                data2.append(k.supplier_id)
                data2.append(k.app_id)
                data2.append(k.end_date)
                data2.append(k.access_type)
                data2.append(k.offer_platform)
                data2.append(k.revenue_model)
                data2.append(k.req_method)
                data2.append(k.revenue_price)
                data1.append(data2)

            print(data1)
            #遍历数组添加到list
            list=[
                dict(
                    id=k[0],
                    offerName=k[1],
                    netName=k[2],
                    appName=k[3],
                    offerUrl=k[4],
                    netId=k[5],
                    supplierId=k[6],
                    appId=k[7],
                    # endDate=k[8],
                    accessType=k[9],
                    offerType=k[10],

                    revenueModel=k[11],
                    requestType=k[12],
                    revenuePrice=k[13]
                ) for k in data1
            ]
            res["data"]=dict(
                    otal=offer.__len__(),
                    pageNum=pageNum,
                    pageSize=pageSize,
                    list=list
                )


        res=json.dumps(res).encode("utf-8").decode('unicode_escape')
        self.write(res)
